package com.sx.pipeline.ui.message

import android.app.Application
import androidx.lifecycle.LifecycleOwner
import com.blankj.utilcode.util.ToastUtils
import com.sx.mvvmlibs.base.BaseViewModel
import com.sx.mvvmlibs.event.SingleLiveEvent
import com.sx.mvvmlibs.http.CommonObserver
import com.sx.mvvmlibs.utils.RxThreadHelper
import com.sx.pipeline.R
import com.sx.pipeline.app.model.CYBaseViewModel
import com.sx.pipeline.repos.http.req.LoginDTO
import com.sx.pipeline.repos.http.req.MessageDTO
import com.sx.pipeline.repos.http.res.*
import com.sx.pipeline.ui.task.entity.ReleaseTaskListVO

/**
 * @Author: YY
 * @Date: 2022/4/13
 * @Description: 消息中心VM
 */
class MessageVM(app: Application) : CYBaseViewModel(app) {

    // 消息类型列表
    val messageTypeListEvent: SingleLiveEvent<List<MessageTypeVo>> = SingleLiveEvent()

    // 消息列表
    val messageListEvent: SingleLiveEvent<Ipage<MessageListVo>> = SingleLiveEvent()

    // 消息详情
    val messageDetailEvent: SingleLiveEvent<MessageDetailVo> = SingleLiveEvent()

    /**
     * 获取单位类型
     */
    fun getNoticeTypeList() {
        addSubscribe(
            mModel.getNoticeTypeList()
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<List<MessageTypeVo>, BaseViewModel<*>>
                    (this, false) {
                    override fun onResult(result: List<MessageTypeVo>) {
                        messageTypeListEvent.postValue(result)
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        ToastUtils.showShort(msg)
                    }
                })
        )
    }

    /**
     * 根据类型获取消息列表
     */
    fun getNoticeByCode(code: Int, page: Int = 1) {
        addSubscribe(mModel.getNoticeByCode(code, page)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<Ipage<MessageListVo>, BaseViewModel<*>>
                    (this, false) {
                    override fun onResult(result: Ipage<MessageListVo>) {
                        messageListEvent.postValue(result)
                        mUI.initRefreshEvent().postValue(true)
                        if (page != 1) {
                            mUI.initLoadEvent().postValue(true)
                        }
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        ToastUtils.showShort(msg)
                        mUI.initRefreshEvent().postValue(false)
                        if (page != 1) {
                            mUI.initLoadEvent().postValue(true)
                        }
                    }
                })
        )
    }

    /**
     * 获取消息详情
     */
    fun getNoticeDetail(id: Int, noticeId: Int) {
        val dto = MessageDTO(id, noticeId)
        addSubscribe(
            mModel.getNoticeDetail(dto)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<MessageDetailVo, BaseViewModel<*>>
                    (this, true) {

                    override fun onResult(result: MessageDetailVo) {
                        messageDetailEvent.postValue(result)
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        ToastUtils.showShort("$msg")

                    }
                })
        )
    }


    /**
     * 全部已读
     */
    val messageReadEvent: SingleLiveEvent<Boolean> = SingleLiveEvent()

    fun getAllHaveRead() {
        addSubscribe(
            mModel.getAllHaveRead()
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<Boolean, BaseViewModel<*>>
                    (this, false) {

                    override fun onResult(result: Boolean) {
                        ToastUtils.showShort("操作成功")
                        messageReadEvent.postValue(true)
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        ToastUtils.showShort("$msg")

                    }
                })
        )
    }
}